# -*- coding:utf-8 -*-
from loguru import logger

from constant.request_contants import REQUEST_GET
from engine.handler.common_handler import ToJsonHandler
from engine.handler.lianjia_handler import LianjiaHandler
from engine.parse_engine import ParseEngine
from engine.save_engine import MongoSaveEngine
from target.target_api import TargetAPI


class LianjiaTask:
    def __init__(self):
        self.source = "lianjia"
        pass

    def start(self, url_list):
        for url in url_list:
            self.__start(url)

    def __start(self, url):
        parse_engine = ParseEngine(target_api=TargetAPI(url, method=REQUEST_GET))
        req_data = url.split("?")[1]
        parse_data = parse_engine.parse(req_data)
        logger.debug(parse_data)

        handle_data = ToJsonHandler().handle(parse_data)
        handle_data = LianjiaHandler().handle(handle_data)
        for d in handle_data:
            d["source"] = self.source
            d["url"] = url + "?" + req_data
            d["city"] = LianjiaHandler.get_city_code(url.split("//")[1][:2])
            d["province"] = LianjiaHandler.get_province_code()

        MongoSaveEngine("c_estate").batch_save(handle_data)
